//
//  LineChartView.m
//  re
//
//  Created by nonoking on 2017/2/8.
//  Copyright © 2017年 nonoking. All rights reserved.
//

#import "FFLineChartView.h"

NSInteger lcPadding = 50;

@interface FFLineChartView()

/**记录竖线的x位置*/
@property(nonatomic,strong)NSMutableArray *xVerticals;

/**记录横线的y位置*/
@property(nonatomic,strong)NSMutableArray *yHorizontal;

@end

@implementation FFLineChartView

- (void)drawRect:(CGRect)rect {
    
    [self drawVerticalLine];
    
    [self drawHorizontalLine];
    
    [self drawCirileAndLine];
    
}


- (void)drawCirileAndLine
{

    CGFloat gaodu = [[self.yHorizontal firstObject] floatValue] - [[self.yHorizontal lastObject] floatValue];
    BOOL firstPoint = YES;
    
    __block typeof(BOOL) weakfirstPoint = firstPoint;
    
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    
    [self.showDates enumerateObjectsUsingBlock:^(NSNumber *obj, NSUInteger idx, BOOL * _Nonnull stop) {
        CGFloat showdata = [obj floatValue];
        CGFloat pointY = [[self.yHorizontal firstObject] floatValue] - (gaodu / 250 * showdata);
        CGFloat pointX = [self.xVerticals[idx + 1] floatValue];
 
        if (weakfirstPoint) {
            CGContextMoveToPoint(ctx, pointX, pointY);
            weakfirstPoint = NO;
        }else{
            CGContextAddLineToPoint(ctx, pointX, pointY);
        }
     
        [self drawCirile:CGPointMake(pointX, pointY) color:[UIColor redColor]];
        
    }];
    
    CGContextStrokePath(ctx);
}

- (void)drawCirile:(CGPoint)center color:(UIColor *)color{
    
    CALayer *roomCirile = [CALayer layer];
    roomCirile.position = center;
    roomCirile.bounds = CGRectMake(0, 0, 6, 6);
    roomCirile.cornerRadius = 3;
    roomCirile.masksToBounds = YES;
    roomCirile.backgroundColor = color.CGColor;
    [self.layer addSublayer:roomCirile];
}

- (void)drawVerticalLine
{
    CGFloat viewW = self.frame.size.width;
    CGFloat viewH = self.frame.size.height;
    
    CGFloat spaceVertical = (viewW - lcPadding - 34) / self.yDescribe.count;
    
    for (NSUInteger idx = 0; idx < self.yDescribe.count + 1; idx++) {
        CGFloat lineX = lcPadding + idx * spaceVertical;
        
        [self.xVerticals addObject:@(lineX)];
        
         // CGContextFillPath(ctx)
        if (idx > 0) {
            UILabel *yLabel = [[UILabel alloc]initWithFrame:CGRectMake(lineX - 20, viewH - lcPadding, 40, 21)];
            yLabel.font = [UIFont systemFontOfSize:12];
            yLabel.text = self.yDescribe[idx - 1];
            yLabel.textAlignment = NSTextAlignmentCenter;
            yLabel.backgroundColor = [UIColor redColor];
            [self addSubview:yLabel];
        }else{
            CGContextRef ctx = UIGraphicsGetCurrentContext();
            CGContextMoveToPoint(ctx, lineX, 10);
            
            CGContextAddLineToPoint(ctx, lineX, viewH - lcPadding);
            CGContextStrokePath(ctx);
        }
        
        
        
    }
}

- (void)drawHorizontalLine
{
    
//    NSLog(@"%@",self.xDescribe);
    CGFloat viewW = self.frame.size.width;
    CGFloat viewH = self.frame.size.height;
    CGContextRef ctx2 = UIGraphicsGetCurrentContext();
    
    CGFloat spaceHorizontal = (viewH - lcPadding) / self.xDescribe.count;
    
    CGFloat horizontalLineX = lcPadding - 5;
    for (NSUInteger idx = 0; idx < self.xDescribe.count; idx++) {
        CGFloat moveX = lcPadding;
        CGFloat moveY = (viewH - lcPadding)- (idx * spaceHorizontal);
        
        [self.yHorizontal addObject:@(moveY)];
        if (idx == 0) {
            moveX = viewW - 34;
        }
        
        CGContextMoveToPoint(ctx2, horizontalLineX , moveY);
        
        CGContextAddLineToPoint(ctx2, moveX, moveY);
        CGContextStrokePath(ctx2);
        
        UILabel *horizontalLabel = [[UILabel alloc]initWithFrame:CGRectMake(horizontalLineX - 43, moveY-10, 40, 21)];
        horizontalLabel.textAlignment = NSTextAlignmentRight;
        horizontalLabel.text = [NSString stringWithFormat:@"%@",self.xDescribe[idx]];
        [self addSubview:horizontalLabel];
    }
}

- (NSMutableArray *)xVerticals
{
    if (_xVerticals == nil) {
        _xVerticals = [NSMutableArray array];
    }
    return _xVerticals;
}

- (NSMutableArray *)yHorizontal
{
    if (_yHorizontal == nil) {
        _yHorizontal = [NSMutableArray array];
    }
    return _yHorizontal;
}


@end
